from datetime import datetime
import pytz
from sqlalchemy import (
    Column,
    Float,
    Index,
    Numeric,
    String,
    TIMESTAMP,
    text,
    ForeignKey,
    Integer,
    Boolean,
    Text,
    DateTime,
    JSON,
    Enum,
)
from sqlalchemy.dialects.mysql import BIGINT, TINYINT, VARCHAR, DATETIME
from sqlalchemy.orm import declarative_base, relationship
import enum

from app.utils.time_util import current_time_with_millis
import uuid


def cst_now():
    tz = pytz.timezone("Asia/Shanghai")
    return datetime.now(tz)


Base = declarative_base()
metadata = Base.metadata


def gen_uuid():
    return str(uuid.uuid4())


class YmUser(Base):
    __tablename__ = "ym_user"

    id = Column(String(64), primary_key=True, comment="主键", default=gen_uuid)

    student_id = Column(
        String(64), nullable=False, server_default=text("''"), comment="学号"
    )
    name = Column(String(32), nullable=False, server_default=text("''"), comment="姓名")

    major = Column(
        String(64), nullable=False, server_default=text("''"), comment="专业"
    )

    pid = Column(
        String(64), nullable=False, server_default=text("''"), comment="身份证号"
    )

    password = Column(
        String(64), nullable=False, server_default=text("''"), comment="身份证号6位"
    )


class UserFeedBack(Base):
    __tablename__ = "user_feedback"

    id = Column(String(36), primary_key=True, default=gen_uuid)
    student_id = Column(String(36))
    content = Column(String(1024))
    created_at = Column(DateTime, default=cst_now)
    status = Column(Integer, default=0, comment="状态 0表示未处理 1表示已处理")


class StudentScore(Base):
    __tablename__ = "student_scores"

    id = Column(String(36), primary_key=True, default=gen_uuid)
    class_name = Column(String(64))
    student_id = Column(String(64), index=True)
    name = Column(String(64))
    course_name = Column(String(128))
    score = Column(Float)
    academic_year = Column(String(32))
    semester = Column(String(16))
    student_type = Column(String(64))
    college = Column(String(128))
    major = Column(String(128))
    grade = Column(String(64))
    student_tag = Column(String(128))
    teaching_college = Column(String(128))
    course_code = Column(String(64))
    teaching_class = Column(String(128))
    teacher = Column(String(64))
    credit = Column(Float)
    score_note = Column(String(128))
    exam_type = Column(String(64))
    gpa = Column(Float)
    course_tag = Column(String(128))
    course_category = Column(String(128))
    course_belonging = Column(String(128))
    course_nature = Column(String(128))
    assessment_method = Column(String(128))
    is_score_invalid = Column(Boolean)
    submitter = Column(String(64))
    submit_time = Column(DateTime)
    is_degree_course = Column(Boolean)
    gender = Column(String(16))
    major_direction = Column(String(128))
    course_english_name = Column(String(128))
    remarks = Column(String(256))
    credit_gpa = Column(Float)
    course_type = Column(String(128))


class StudentStatistic(Base):
    __tablename__ = "student_statistics"

    student_id = Column(String(64), primary_key=True)
    name = Column(String(64))
    student_tag = Column(String(256))
    major = Column(String(128))

    course_count = Column(Integer)
    total_credit = Column(Float)
    total_credit_gpa = Column(Float)

    # 排序
    gpa_rank = Column(Integer)
    # 专业最高的gpa均值
    major_top_gpa = Column(Float)

    ranked_score = Column(Numeric(12, 8))  # 总位数12，保留小数点后8位
    avg_gpa = Column(Numeric(12, 8))
